package cn.jinjimi.app.controller;

import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import bolts.Task;
import cn.jinjimi.app.entity.LinkBean;
import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.domain.data.net.API;
import cn.jinjimi.domain.data.net.AdvService;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 *
 * @author zhenhua
 * @date 2018/1/28
 */

public class AdvController {

    public static Task<List<LinkBean>> getAdvList(final String position) {
        return Task.callInBackground(new Callable<List<LinkBean>>() {
            @Override
            public List<LinkBean> call() throws Exception {
                Call<ResponseBody> call = API.create(AdvService.class).getAdvs(position);
                Response<ResponseBody> res = call.execute();
                if (res != null && res.isSuccessful()) {
                    Type type = new TypeToken<List<LinkBean>>(){}.getType();
                    List<LinkBean> advs = GsonUtils.fromJson(res.body().string(), type);
                    if (advs != null) {
                        return advs;
                    }
                }
                return new ArrayList<>();
            }
        });
    }
}
